package com.duanjw.springboot.shardingsphere.controller;

import java.util.Map;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.duanjw.springboot.shardingsphere.common.R;
import com.duanjw.springboot.shardingsphere.pojo.entity.Teacher;
import com.duanjw.springboot.shardingsphere.service.TeacherService;

import lombok.AllArgsConstructor;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author duanjw
 * @since 2019-06-04
 */
@RestController
@RequestMapping("/teacher")
@AllArgsConstructor
public class TeacherController {

    private final TeacherService teacherService;

    /**
     * 新增
     * curl -X POST -H 'Content-Type: application/json' -d '{"id":"1","name":"1"}' localhost:8080/teacher
     * @param teacher 实体
     * @return success/false
     */
    @PostMapping
    public R<Boolean> save(@RequestBody Teacher teacher) {
        return R.ok(teacherService.save(teacher));
    }

    /**
     * 根据id查看
     * curl localhost:8080/teacher/1
     * @param id ID
     * @return Teacher
     */
    @GetMapping("/{id}")
    public R<Teacher> getById(@PathVariable Long id) {
        return R.ok(teacherService.getById(id));
    }

    /**
     * 分页查询
     *
     * @param params 分页对象
     * @return 分页对象
     */
    @GetMapping("/page")
    public Page page(@RequestParam Map<String, Object> params) {
        return teacherService.page(new Page<>());
    }

    /**
     * 根据id删除
     *
     * @param id ID
     * @return success/false
     */
    @DeleteMapping("/{id}")
    public R<Boolean> removeById(@PathVariable Long id) {
        return R.ok(teacherService.removeById(id));
    }

    /**
     * 根据id修改
     *
     * @param teacher 实体
     * @return success/false
     */
    @PutMapping
    public R<Boolean> updateById(@RequestBody Teacher teacher) {
        return R.ok(teacherService.updateById(teacher));

    }

    @PostMapping("join")
    public R<Teacher> updateAndGet(@RequestBody Teacher teacher) {
        return R.ok(teacherService.updateAndGet(teacher));
    }

    /**
     * 新增
     *
     * @param teacher 实体
     * @return success/false
     */
    @PostMapping("thread")
    public R<Boolean> saveThread(@RequestBody Teacher teacher) {
        // 线程池
        int THREAD_POOL_SIZE = 5;
        ExecutorService executorService = new ThreadPoolExecutor(THREAD_POOL_SIZE, THREAD_POOL_SIZE,
                0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<>(100));
        CompletionService<R<Boolean>> completionService = new ExecutorCompletionService<>(
                executorService);
        for (int i = 0; i < 100; i++) {
            completionService.submit(() -> save(teacher));
        }
        try {
            for (int i = 0; i < 100; i++) {
                R<Boolean> booleanR = completionService.take().get();
                System.out.println(booleanR);
            }
        } catch (InterruptedException | ExecutionException e) {
            System.out.printf("获取返回值异常");
        }
        executorService.shutdown();
        return R.ok(true);
//        return R.ok(teacherService.save(teacher));
    }

}
